package by.alex.dzeshko.facebooksuperclient;

import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import by.alex.dzeshko.facebooksuperclient.api.ApiRequest;
import by.alex.dzeshko.facebooksuperclient.http.HttpManager;
import by.alex.dzeshko.facebooksuperclient.models.FbUser;
import by.alex.dzeshko.facebooksuperclient.oauth.FBOAuthHelper;
import by.alex.dzeshko.facebooksuperclient.utils.InetChecker;

public class AuthActivity extends Activity{

	private String LOG_TAG = "AuthActivity";
	
	private WebView mWebView;
	
	private ProgressBar mPrBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auth);
		InetChecker.checkInetConnection(this);
		mPrBar = (ProgressBar) findViewById(R.id.progressBar);
		mWebView = (WebView) findViewById(R.id.wvAuth);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setWebViewClient(new WebViewClient(){

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				mPrBar.setVisibility(View.INVISIBLE);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				mPrBar.setVisibility(View.VISIBLE);
				if(FBOAuthHelper.isRedirectURL(url)){
					mPrBar.setVisibility(View.INVISIBLE);
					mWebView.setClickable(false);
					String token = FBOAuthHelper.saveCreditionals(AuthActivity.this, url);
					new AsyncTask<String, Void, Void>() {

						@Override
						protected Void doInBackground(String... params) {
							String jo;
							try {
								jo = HttpManager.getInstance().loadAsString(ApiRequest.getUserId()+params[0]);
								FbUser user = new FbUser(jo);
								String id = String.valueOf(user.getId());
								FBOAuthHelper.userId = id;
								SharedPreferences prefs = getSharedPreferences(FBOAuthHelper.AUTH_PREFS, Context.MODE_PRIVATE);
								Editor editor = prefs.edit();
								if(FBOAuthHelper.userId!=null){
									editor.putString(FBOAuthHelper.USER_ID, FBOAuthHelper.userId);
									editor.commit();
									}
							} catch (IOException e) {
								Log.e(LOG_TAG, e.toString());
							}
							return null;
						}

						@Override
						protected void onPostExecute(Void result) {
							super.onPostExecute(result);
							startActivity(new Intent(AuthActivity.this, FeedMainActivity.class));
							finish();
						}

						@Override
						protected void onPreExecute() {
							super.onPreExecute();
							mPrBar.setVisibility(View.VISIBLE);
							mWebView.setVisibility(View.INVISIBLE);
						}
					}.execute(token);
				}
			}
		});
		
		mWebView.loadUrl(FBOAuthHelper.getLoginUrl());

	}

	
}
